package com.example.kwokg.meishi.Activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import com.example.kwokg.meishi.R;
import com.example.kwokg.meishi.user.User;

import cn.bmob.v3.BmobUser;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.LogInListener;

/**
 * Created by kwokg on 2016/9/18.
 */
public class Login extends AppCompatActivity{

    private EditText edt_name,edt_password;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login);
        setTitle("登录");
        
        edt_name = (EditText) findViewById(R.id.login_username);
        edt_password = (EditText) findViewById(R.id.login_password);
    }

    //对比数据库是否可以登录
    public void checkUser(View view) {
        
        //TODO:文本框不允许输入非法字符
        final String username=edt_name.getText().toString();
        String password=edt_password.getText().toString();
        
        //判断编辑框是否为空
        if (username.equals("")||password.equals("")){
            Toast.makeText(Login.this, "请检查输入内容是否为空！", Toast.LENGTH_SHORT).show();
        }else{
            BmobUser.loginByAccount(username, password, new LogInListener<User>() {
                @Override
                public void done(User user, BmobException e) {
                    if (e==null){
                        Toast.makeText(Login.this, "登录成功！", Toast.LENGTH_SHORT).show();
                        //传值跳转
                        startActivity(new Intent(Login.this,MainActivity.class).putExtra("name",username));
                        overridePendingTransition(0,0);
                        finish();
                    }else{
                        Toast.makeText(Login.this, "登录失败~", Toast.LENGTH_SHORT).show();
                    }
                }
            });
        }
    }

    //登录页面跳转注册页面
    public void toRegister(View view) {
        startActivity(new Intent(this,Register.class));
        overridePendingTransition(0,0);
        finish();
    }
    @Override
    protected void onPause() {
        super.onPause();
        overridePendingTransition(0,0);
    }
}
